package org.zjx.app.strategy.impl;


import org.zjx.analysis.analyze.ImageAnalyzer;
import org.zjx.analysis.analyze.login.*;
import org.zjx.analysis.analyze.manager.LoginAnalysisManager;
import org.zjx.app.action.login.*;
import org.zjx.window.action.IAction;
import org.zjx.window.action.impl.DelayAction;
import org.zjx.window.strategy.IStrategy;
import org.zjx.window.window.IWindow;
import org.zjx.window.window.impl.LoginWindow;

import java.util.Optional;

public class LoginStrategy implements IStrategy {

    @Override
    public IAction analyze(IWindow window) {

        LoginAnalysisManager manager = LoginAnalysisManager.getInstance();
        Optional<ImageAnalyzer> matchedAnalyzer = manager.findMatchedAnalyzer(window.getScreenshot());

        if (matchedAnalyzer.isPresent()){
            ImageAnalyzer analyzer = matchedAnalyzer.get();
            if (analyzer instanceof LoginHomeAnalysis){
                return new LoginHomeAction(window);
            }else if (analyzer instanceof LoginToggleAnalysis){
                return new LoginToggleAction(window);
            }else if (analyzer instanceof LoginAccountAnalysis){
                return new LoginAccountAction(window);
            }else if (analyzer instanceof LoginServerAnalysis){
                return new LoginServerAction(window);
            }else if (analyzer instanceof LoginRoleAnalysis){
                return new LoginRoleAction(window);
            }else if (analyzer instanceof LoginCreateAnalysis){
                return new LoginCreateAction(window);
            }else if (analyzer instanceof LoginChooseTypeAnalysis){
                return new LoginChooseTypeAction(window);
            }
        }
        String title = window.getTitle();
        if (title.contains("[")){
            window.shutdown();
        }

        return new DelayAction(window,1000);
    }

    @Override
    public String getName() {
        return "登录";
    }

    @Override
    public int getPriority() {
        return 40;
    }

    @Override
    public boolean isApplicable(IWindow window) {
        return window instanceof LoginWindow;
    }
}
